﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using HuoTu.Common;
using HuoTu.Data;
using NSoup;
namespace ExampleWin
{
    public partial class finddomain : Form
    {
        private List<string> domainList = new List<string>();
        private int curCount = 0;
        private string prev = "";
        private string end = "";
        public finddomain()
        {
            InitializeComponent();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            btnGo.Enabled = false;
            prev = txtPrev.Text;
            end = txtEnd.Text;
            curCount = 0;
            if (domainList.Count > 0) domainList.Clear();


            string t1 = @"abcdefghijklmnopqrstuvwxyz";
            for (var i = 0; i < t1.Length; i++)
            {
                for (var n = 0; n < t1.Length; n++)
                {
                    domainList.Add(prev + t1.Substring(i, 1) 
                        + t1.Substring(n, 1) + end);
                }
            }

            timer1.Enabled = true;
            timer1.Interval = 1000;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (curCount == domainList.Count)
            {
                timer1.Enabled = false;
                btnGo.Enabled = true;
                return;
            }
            string domainName = domainList[curCount];
            DomainUnity d = new DomainUnity();
            d.DoRequest(domainName);
            if (d.Result == ResultStatus.NOREGED)
                textBox1.AppendText(string.Format(@"{0}
"
                    , domainName));
            curCount++;
        }

        private void finddomain_Load(object sender, EventArgs e)
        {

        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            btnGo.Enabled = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }
    }

    public class DomainUnity
    {
        public string RequestUrl = @"http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=";
        public ResultStatus Result;
        /// <summary>
        /// 处理请求
        /// </summary>
        public void DoRequest(string domainName)
        {
            string html = SysHtml.HttpWebResponse(RequestUrl + domainName);
            //解析json窜
            //NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html);
            //NSoup.Nodes.Element element = doc.GetElementById("original");

            if (html.IndexOf(((int)ResultStatus.NOREGED).ToString()) >= 0)
                Result = ResultStatus.NOREGED;
            else
                Result = ResultStatus.REGED;

        }
    }

    public enum ResultStatus
    {
        /// <summary>
        /// 未注册
        /// </summary>
        [Description("未注册")]
        NOREGED = 210,
        /// <summary>
        /// 注册
        /// </summary>
        [Description("注册")]
        REGED = 211

    }
}
